﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Contents
{
    /// <summary>
    /// This object represents the methods of a Download.
    /// </summary>
    internal class DownloadDAO
    {

        #region general

        private static Download LoadObj(IDataReader datareader)
        {
            Download obj = new Download();
            obj.DownloadID = MpDataHelp.GetInt(datareader, "DownloadID");
            obj.CategoryID = MpDataHelp.GetInt(datareader, "CategoryID");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.Count = MpDataHelp.GetInt(datareader, "Count");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Link = MpDataHelp.GetString(datareader, "Link");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Image = MpDataHelp.GetString(datareader, "Image");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            obj.FormatUrl = MpDataHelp.GetString(datareader, "FormatUrl");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");	
            return obj;
        }

        #endregion

        public static int Download_Add(Download obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Download_Add");
            db.AddInParameter(dbCommand, "DownloadID", DbType.Int32, obj.DownloadID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Count", DbType.Int32, obj.Count);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Link", DbType.String, obj.Link);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@DownloadID"));
            return -1;
        }
        public static int Download_Update(Download obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Download_Update");
            db.AddInParameter(dbCommand, "DownloadID", DbType.Int32, obj.DownloadID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Count", DbType.Int32, obj.Count);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Link", DbType.String, obj.Link);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Image", DbType.String, obj.Image);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Download_Delete(Int32 DownloadID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Download_Delete");
            db.AddInParameter(dbCommand, "DownloadID", DbType.Int32, DownloadID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Download> Download_List(int DownloadID, int CategoryID, int SortOrder, string Title, bool IsActive)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Download_List");
            db.AddInParameter(dbCommand, "DownloadID", DbType.Int32, DownloadID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            db.AddInParameter(dbCommand, "Title", DbType.String, Title);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            List<Download> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Download>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}



